var map;
var image = base_url + 'assets/img/google/bed-icon-small.png';
var service_url = base_url + lang + '/hotels/' + google_object_method + '/'
var infowindow;
var bounds;
var markersArray = [];

var MAP_INITIALIZED = false;
var MARKER_ID = 0;

function initialize() {

    if (!LOADMAP)
        return;

    infowindow = new google.maps.InfoWindow({});
    bounds = new google.maps.LatLngBounds();

    var mapOptions = {
        center: new google.maps.LatLng(42.093828, 19.098272),
        zoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        panControl: true,
        zoomControl: true,
        mapTypeControl: false,
        scaleControl: true,
        streetViewControl: false,
        overviewMapControl: false,
        scrollwheel: false


    };

    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    // go trough each hotel Array and add marker
    var cnt = hotelsArray.length;

    for (var i = 0; i < cnt; i++) {

        var latLang = new google.maps.LatLng(hotelsArray[i].x, hotelsArray[i].y);

        addMarker(
                latLang,
                hotelsArray[i].name,
                hotelsArray[i].ID
                );

        bounds.extend(latLang);

    }



    map.fitBounds(bounds);

    function addMarker(location, title, hotelID) {

        marker = new google.maps.Marker({
            position: location,
            map: map,
            icon: image,
            title: title,
            hotspotid: hotelID
        });

        markersArray.push(marker);

        //log(title+": "+hotelID)

        // add an event listener for this marker
        bindInfoWindow(marker, map, infowindow);

    }

    function bindInfoWindow(marker, map, infowindow) {
        google.maps.event.addListener(marker, 'click', function() {

            $.ajax({
                url: service_url + marker.hotspotid + '?' + get_arr,
                dataType: 'json',
                success: function(data) {


                    if (data.action == false && data.msg == 'session_expired') {
                        window.location.reload();
                    } else {
                        infowindow.setContent(data.html);
                        infowindow.open(map, marker);
                    }
                }
            });

        });
    }

    cbHTML = $("#google_box").html();

    //console.log(MAP_INITIALIZED)

    if (MAP_INITIALIZED == false) {

        MAP_INITIALIZED = true;

        $.ajax({
            url: service_url + markersArray[MARKER_ID].hotspotid + '?' + get_arr,
            dataType: 'json',
            success: function(data) {


                if (data.action == false && data.msg == 'session_expired') {
                    window.location.reload();
                } else {
                    infowindow.setContent(data.html);
                    infowindow.open(map, markersArray[MARKER_ID]);
                }
            }
        });
    }

}

$(document).ready(function() {

    if (LOADMAP) {

        var offsetWinWidth = 100;
        var offsetWinHeight = 100;

        $('.GH').live('click', function() {
            var index = $(this).attr('data-holder');
            //log(index)

            $.ajax({
                url: service_url + markersArray[index].hotspotid + '?' + get_arr,
                dataType: 'json',
                success: function(data) {


                    if (data.action == false && data.msg == 'session_expired') {
                        window.location.reload();
                    } else {
                        infowindow.setContent(data.html);
                        infowindow.open(map, markersArray[index]);
                    }
                }
            });


        })

        gmpapW = $(window).innerWidth() - offsetWinWidth - 410 - 10;
        gmpapH = $(window).innerHeight() - offsetWinHeight;

        $("#google_map").css('width', gmpapW);
        $("#google_map").css('height', gmpapH);
        $("#google-list").css('height', gmpapH - 31);

        var cbHTML = $("#google_box").html();
        $("#google_box").remove();

        $(".googlebox").colorbox({
            html: "<div id='google_box'>" + cbHTML + "</div>",
            scrolling: false,
            width: $(window).innerWidth() - offsetWinWidth + "px",
            height: $(window).innerHeight() - offsetWinHeight + "px",
            onComplete: function() {

                //console.log('.googlebox')
                MAP_INITIALIZED = true;
                initialize();


            }
        });

        $('.show_map_object').live('click', function(e) {

            e.preventDefault();
            var elem = this;

            $.colorbox({
                html: "<div id='google_box'>" + cbHTML + "</div>",
                scrolling: false,
                width: $(window).innerWidth() - offsetWinWidth + "px",
                height: $(window).innerHeight() - offsetWinHeight + "px",
                onComplete: function() {

                    var index = $(elem).attr('data-holder');

                    if (MAP_INITIALIZED) {
                        $.ajax({
                            url: service_url + markersArray[index].hotspotid + '?' + get_arr,
                            dataType: 'json',
                            success: function(data) {


                                if (data.action == false && data.msg == 'session_expired') {
                                    window.location.reload();
                                } else {
                                    infowindow.setContent(data.html);
                                    infowindow.open(map, markersArray[index]);
                                }
                            }
                        });
                    } else {
                        MARKER_ID = index;
                    }

                    initialize();

                }
            });



        });

        // go trough each hotel Array and add marker
        var cnt = hotelsArray.length;
        bounds2 = new google.maps.LatLngBounds();

        for (var i = 0; i < cnt; i++) {

            var latLang = new google.maps.LatLng(hotelsArray[i].x, hotelsArray[i].y);

            bounds2.extend(latLang);

        }

        var x = bounds2.getCenter().hb;
        var y = bounds2.getCenter().ib;

        $('.side-google-map').css('background-image', 'url("http://maps.googleapis.com/maps/api/staticmap?key=AIzaSyACIkfLKjw2w9u2V9XKJUqnFqfIirp6Bsc&sensor=false&size=250x205&center=' + x + ',' + y + '&zoom=11")')




        // Initilaze FILTERS
        // Do filters
        $('.filterelement').live('click', function() {
            var hasActiveClass = $(this).hasClass('active');
            if (hasActiveClass) {
                $(this).removeClass('active');
                $(this).find('.filter_meta').html(' ');
            } else {
                $(this).addClass('active');
                $(this).find('.filter_meta').html(' active ');
            }
            $("#filterbox_options").mask("Filtering...");
        })

    }


});